package com.wonders.cyxtjsonupdate;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wonders.cyxtjsonupdate.entity.Cyxt;
import com.wonders.cyxtjsonupdate.service.CyxtJsonData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@SpringBootTest
class CyxtJsonUpdateApplicationTests4 {

    //---------每车日均订单（单）

    @Autowired
    CyxtJsonData cyxtJsonData;

    @Test
    void test1() {

        Calendar cal = Calendar.getInstance();
        DecimalFormat df = new DecimalFormat("0.00");
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(0);//保留0位
        ArrayList<String> list = new ArrayList<>();//用来存储所有的最终的json
        //获得当前月份
        int month = new Date().getMonth();
        month = 11;//因目前1月份数据库未产生12月份数据，此此先模拟
        //获得当前年份
        int year = cal.get(Calendar.YEAR);
        year = 2021;

        int totalMonad = 0;//用来统计出租车的总单量
        long totalkm = 0;//用来统计出租车的月总公里


        //---每车日均订单（单）
        Object carAvg = cyxtJsonData.initCarAvg();
        //统计每辆车的总营收
        List<Map<String, Object>> moneyList = cyxtJsonData.initTotalMoney();
        //每单载客里程
        Object data = cyxtJsonData.initSingleData();
        //每单载客时长
        Object orderTime = cyxtJsonData.initPassengerTimeFororder();
        //每单营收
        Object money = cyxtJsonData.initMoneyForOrder();
        //每车日均载客时长（分）
        Object kmOfPassengers = cyxtJsonData.initKiloMeterForCarryPassengers();
        //每笔订单营收分布
        List<Map<String, Object>> orderList = cyxtJsonData.initRevenueDistributionOforder();
        //【巡游车】每车日均营收分布
        List<Map<String, Object>> carMoneyList = cyxtJsonData.initDistributionOfCar();
        //巡游车日均成距分布
        List<Map<String, Object>> distanceList = cyxtJsonData.initDistributionOfDistance();
        //出租车总量
        Object taxiOrder = cyxtJsonData.buildtaxi();
        //--【巡游车】企业日均订单TOP10
        List<Map<String, Object>> taxiTopTen = cyxtJsonData.initTaxiTopTen();
        //网约车日均订单
        Object netcarOrder = cyxtJsonData.buildNetcarOrder();
        //网约车每单载客里程（公里）
        Object netCardis = cyxtJsonData.buildNetcarOrderOfdis();
        //网约车--每单载客时长（分）
        Object netCarTime = cyxtJsonData.buildNetcarOrderOfTime();
        //网约车每单营收
        Object netCarRich = cyxtJsonData.buildNetcarOrderOfRich();
        //网约车每车日均载客里程（公里）
        Object netcardisOfDay = cyxtJsonData.buildNetcardisOfDay();
        //网约车--每车日均载客时长
        Object worktime = cyxtJsonData.buildNetcarWorktime();
        //网约车订单日均分布
        List<Map<String, Object>> distributionOrderList  = cyxtJsonData.initDistributionOfOrders();



        //每车日均订单
        Cyxt cyxt1 = buildgeneralpParame("be6b6c098d0e4707a4a65df460adef11", "gtjtLazy", "xmcrjdd");
        buildPassengermileage(cyxt1, carAvg, list, df);

        //每单载客公里数(公里)
        Cyxt cyxt2 = buildgeneralpParame("9f1f20aad37c43789cf3449dcf5e9717", "gtjtLazy", "xmdzklc");
        buildPassengermileage(cyxt2, data, list, df);

        //--每单载客时长（分）
        Cyxt cyxt3 = buildgeneralpParame("f9bd5a7d85654dd4b581e61f814f7b6c", "gtjtLazy", "xmdzksc");
        buildPassengermileage(cyxt3, orderTime, list, df);

        //--每单营收（元）
        Cyxt cyxt4 = buildgeneralpParame("072deda380df4a28b674692d89a1ddcd", "gtjtLazy", "xmdys");
        buildPassengermileage(cyxt4, money, list, df);


        //--每车日均载客里程（公里）
        Cyxt cyxt5 = buildgeneralpParame("d7546cb9cd074517ac4a5ddb4d1febac", "gtjtLazy", "xmcrjzklc");
        buildPassengermileage(cyxt5, data, list, df);
        //--每车日均载客时长（分）
        Cyxt cyxt6 = buildgeneralpParame("1ae3492b302049aeb7e64270b231c22e", "gtjtLazy", "xmcrjzksc");
        buildPassengermileage(cyxt6, kmOfPassengers, list, df);
        //每笔订单营收分布
        Cyxt cyxt7 = buildgeneralpParame("8fed443e0a1e43aba0a5659208b54977", "gtjtLazy", "myChart25");
        buildRevenueDistributionOforder(cyxt7, list, orderList);


        //每车日均营收分布
        Cyxt cyxt8 = buildgeneralpParame("b1e799ee5ba8489ebc76afb43190dade", "gtjtLazy", "myChart22");
        buildDistributionOfCar(cyxt8, list, carMoneyList);

        //日均乘距分布(公里)
        Cyxt cyxt9 = buildgeneralpParame("938c45f4508545da8f2c9c4e5b63c28c", "gtjtLazy", "myChart2");
        buildDistanceOfCar(cyxt9, list, distanceList);
        //--【巡游车】企业日均订单TOP10
        Cyxt cyxt10 = buildgeneralpParame("cac8a92992ac4505b5e3fa9e17d6328e", "gtjtLazy", "myChart4");
        buildTopTen(cyxt10,taxiTopTen,list);
        //网约车日均订单
        Cyxt cyxt11 = buildgeneralpParame("0c72cc09630b46a2a671bdd2cdc5eb38", "gtjtLazy", "wmcrjdd");
        buildPassengermileage(cyxt11,netcarOrder,list,df);
        //网约车每单载客里程（公里）
        Cyxt cyxt12 = buildgeneralpParame("0cc6aed5f72046c893bf78b7d934324e", "gtjtLazy", "wmdzklc");
        buildPassengermileage(cyxt12,netCardis,list,df);
        //网约车每单载客时长（分）
        Cyxt cyxt13 = buildgeneralpParame("b30612d4d41240f3a85bbe3a34333b60", "gtjtLazy", "wmdzksc");
        buildPassengermileage(cyxt13,netCarTime,list,df);
        //网约车每单营收（元）
        Cyxt cyxt14 = buildgeneralpParame("076a85b3e246485895e5b213d1fbe8b9", "gtjtLazy", "wmdys");
        buildPassengermileage(cyxt14,netCarRich,list,df);
        //网约车每车日均载客里程（公里）
        Cyxt cyxt15 = buildgeneralpParame("8e10f63ca6fc4aafaee0936514bc870c", "gtjtLazy", "wmcrjzklc");
        buildPassengermileage(cyxt15,netcardisOfDay,list,df);
        //--每车日均载客时长（分
        Cyxt cyxt16 = buildgeneralpParame("1e024ed3acb744688237807c4326be24", "gtjtLazy", "wmcrjzksc");
        buildPassengermileage(cyxt16,worktime,list,df);
        //-------订单日均分布
        Cyxt cyxt17 = buildgeneralpParame("f0ce478abead49bd9507d6d6023d9fc6", "gtjtLazy", "myChart9");
        buildAveragedailyDistributionOfOrders(cyxt17,distributionOrderList,list);

        System.out.println(list);
    }

    /**
     * Methad info:   //---每车日均订单（单）
     *
     * @author 陈兆庆
     * @date 2022/1/6 18:04
     */
    public int buildCarAvg(Cyxt cyxt, int year, int month, List<Map<String, Object>> dataList, List list, DecimalFormat df) {
        int total = 0;
        for (Map<String, Object> stringObjectMap : dataList) {
            total = total + Integer.parseInt(stringObjectMap.get("COUNT").toString());
        }
        int days = (getDays(year, month)) - 1;
        String s = df.format((float) total / dataList.size() / days);
        cyxt.setDataItem1(s);
        String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(data);
        return total;
    }

    /**
     * Methad info:--每单载客里程（公里）
     *
     * @author 陈兆庆
     * @date 2022/1/6 18:28
     */

    public void buildPassengermileage(Cyxt cyxt, Object data, List list, DecimalFormat df) {
        cyxt.setDataItem1(df.format(Float.parseFloat(data.toString())));
        String datastr = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(datastr);
    }


    /**
     * Methad info:--每单营收（元）
     *
     * @author 陈兆庆
     * @date 2022/1/6 18:52
     */
    public void buildMoney(Cyxt cyxt, int total, List<Map<String, Object>> dataList, List list, DecimalFormat df) {
        long money = 0;
        for (Map<String, Object> stringObjectMap : dataList) {
            money = money + Long.valueOf(stringObjectMap.get("SUM(CCJE)").toString());
        }
        String s = df.format((float) money / total / 10);
        cyxt.setDataItem1(s);
        String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(data);
    }

    /**
     * Methad info:--每车日均载客里程（公里）
     *
     * @author 陈兆庆
     * @date 2022/1/6 19:03
     */
    public void buildDailyPassengerKilometer(Cyxt cyxt, int month, int year, List<Map<String, Object>> taxiList, long total, DecimalFormat df, List list) {
        String s = df.format((float) total / taxiList.size() / 10 / getDays(year, month));
        cyxt.setDataItem1(s);
        String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(data);
    }


    /**
     * Methad info:每笔订单营收分布
     *
     * @author 陈兆庆
     * @date 2022/1/10 10:22
     */
    public void buildRevenueDistributionOforder(Cyxt cyxt, List list, List<Map<String, Object>> orderMaps) {
        String value1 = null;//15元及以下
        String value2 = null;//15-30元
        String value3 = null;//30-50元
        String value4 = null;//50-100元
        String value5 = null;//100元以上

        for (Map<String, Object> entry : orderMaps) {
            if (entry.get("TYPE").equals("150")) {
                value1 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("150300")) {
                value2 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("300500")) {
                value3 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("5001000")) {
                value4 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            }
        }
        value5 = String.format("%.2f", 100 - Double.valueOf(value1) - Double.valueOf(value2) - Double.valueOf(value3) - Double.valueOf(value4));
        cyxt.setDataItem1("[{value:" + value1 + ",name:'15元及以下'},{value:" + value2 + ",name:'15-30元'},{value:" + value3 + ",name:'30-50元'},{value:" + value4 + ",name:'50-100元'},{value:" + value5 + ",name:'100元以上'}]");
        String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(data);
    }

    /**
     * Methad info: 每车日均营收分布
     *
     * @author 陈兆庆
     * @date 2022/1/10 11:04
     */
    public void buildDistributionOfCar(Cyxt cyxt, List list, List<Map<String, Object>> orderMaps) {
        String value1 = null;//100元及以下
        String value2 = null;//100-200元
        String value3 = null;//200-500元
        String value4 = null;//500-1000元
        String value5 = null;//1000元以上
        for (Map<String, Object> entry : orderMaps) {
            if (entry.get("TYPE").equals("100")) {
                value1 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("100200")) {
                value2 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("200500")) {
                value3 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("5001000")) {
                value4 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            }
        }
        value5 = String.format("%.2f", 100 - Double.valueOf(value1) - Double.valueOf(value2) - Double.valueOf(value3) - Double.valueOf(value4));
        cyxt.setDataItem1("[{value:" + value1 + ",name:'100元及以下'},{value:" + value2 + ",name:'100-200元'},{value:" + value3 + ",name:'200-500元'},{value:" + value4 + ",name:'500-1000元'},{value:" + value5 + ",name:'1000元以上'}]");
        String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(data);
    }

    /**
     * Methad info:日均成距分布
     *
     * @author 陈兆庆
     * @date 2022/1/10 11:38
     */
    public void buildDistanceOfCar(Cyxt cyxt, List list, List<Map<String, Object>> orderMaps) {
        String value1 = null;//0-3公里及以下
        String value2 = null;//3-5公里及以下
        String value3 = null;//5-7公里及以下
        String value4 = null;//7-10公里及以下
        String value5 = null;//10-15公里及以下
        String value6 = null;//15-20公里及以下
        String value7 = null;//20-30公里及以下
        String value8 = null;//30-40公里及以下
        String value9 = null;//40-70公里及以下
        String value10 = null;//70公里以上
        for (Map<String, Object> entry : orderMaps) {
            if (entry.get("TYPE").equals("3")) {
                value1 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("35")) {
                value2 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("57")) {
                value3 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("710")) {
                value4 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("1015")) {
                value5 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("1520")) {
                value6 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("2030")) {
                value7 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("3040")) {
                value8 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            } else if (entry.get("TYPE").equals("4070")) {
                value9 = String.format("%.2f", Double.valueOf(entry.get("V").toString()));
            }
        }
        value10 = String.format("%.2f", 100 - Double.valueOf(value1) - Double.valueOf(value2) - Double.valueOf(value3) - Double.valueOf(value4) - Double.valueOf(value5) - Double.valueOf(value6) - Double.valueOf(value7) - Double.valueOf(value8) - Double.valueOf(value9));
        cyxt.setDataItem1("['0-3','3-5','5-7','7-10','10-15','15-20','20-30','30-40','40-70','>70']");
        cyxt.setDataItem2("[" + value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5 + "," + value6 + "," + value7 + "," + value8 + "," + value9 + "," + value10 + "]");
        String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(data);
    }

    /**
     * Methad info:--【巡游车】企业日均订单TOP10
     *
     * @author 陈兆庆
     * @date 2022/1/10 12:31
     */

    public void buildTopTen(Cyxt cyxt, List<Map<String, Object>> taxiTopTen, List list) {
        List<String> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, Object> map = taxiTopTen.get(i);
            if (map.get("YHMC").toString().substring(1, 5).equals("0335")) {
                dataList.add("'大众交通'");
            } else if (map.get("YHMC").toString().substring(1, 5).equals("0216")) {
                dataList.add("'上海强生'");
            } else if (map.get("YHMC").toString().substring(1, 5).equals("0209")) {
                dataList.add("'上海海博'");
            } else if (map.get("YHMC").toString().substring(1, 5).equals("0407")) {
                dataList.add("'上海锦江'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0057")) {
                dataList.add("'宝山海博'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0805")) {
                dataList.add("'衡山汽车'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0010")) {
                dataList.add("'日升出租'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0205")) {
                dataList.add("'迎宾出租'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0058")) {
                dataList.add("'奉贤汽车'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0054")) {
                dataList.add("'青浦海博'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("1416")) {
                dataList.add("'上海旗忠'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0120")) {
                dataList.add("'上海建工'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("0023")) {
                dataList.add("'上海振星'");
            }else if (map.get("YHMC").toString().substring(1, 5).equals("4009")) {
                dataList.add("'上海振东'");
            }
        }
        Object buildtaxi = cyxtJsonData.buildtaxi();//运输总量
         int total=(int)(Double.valueOf(buildtaxi.toString())*10000);
        cyxt.setDataItem1(dataList.toString().replace(" ",""));
        ArrayList<Integer> list1 = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, Object> map = taxiTopTen.get(i);
            Object number = map.get("A.C||'%'");
            double num1=Double.valueOf(number.toString().substring(0,number.toString().length()-1))/100;
             int num3=(int)(num1*total);
            list1.add(num3);
        }
        cyxt.setDataItem2(list1.toString().replace(" ",""));


        String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(data);

    }

    /**
     * Methad info:-------订单日均分布
     * @author 陈兆庆
     * @date 2022/1/10 15:44
     */
   public void buildAveragedailyDistributionOfOrders(Cyxt cyxt,List<Map<String, Object>> distributionOrderList,List list){
       List<String> dataList = new ArrayList<>();
       ArrayList<String> list1 = new ArrayList<>();
       for (int i = 0; i < 10; i++) {
           Map<String, Object> map = distributionOrderList.get(i);
           if (map.get("COMPANYID").toString().equals("didi")) {
               dataList.add("'滴滴'");
           } else if (map.get("COMPANYID").toString().equals("meituan")) {
               dataList.add("'路团'");
           } else if (map.get("COMPANYID").toString().substring(1, 5).equals("3201NJLXPJXB")) {
               dataList.add("'领行'");
           } else if (map.get("COMPANYID").toString().equals("3100SKCX7762")) {
               dataList.add("'赛可'");
           }else if (map.get("COMPANYID").toString().equals("3100SMLYMM4J")) {
               dataList.add("'申茂'");
           }else if (map.get("COMPANYID").toString().equals("3301YXKJ223E")) {
               dataList.add("'优行'");
           }else if (map.get("COMPANYID").toString().equals("shouyue")) {
               dataList.add("'首约'");
           }else if (map.get("COMPANYID").toString().equals("shenzhou")) {
               dataList.add("'神舟'");
           }else if (map.get("COMPANYID").toString().equals("3502FZXTRM1E")) {
               dataList.add("'方舟'");
           }else if (map.get("COMPANYID").toString().equals("3301TMCX463C")) {
               dataList.add("'添猫'");
           }else if (map.get("COMPANYID").toString().equals("yangguangchedao")) {
               dataList.add("'阳光环球'");
           }else if (map.get("COMPANYID").toString().equals("1200HTCYEH7R")) {
               dataList.add("'海豚出游'");
           }else if (map.get("COMPANYID").toString().equals("4201BMKP074X")) {
               dataList.add("'阳光环球'");
           }else if (map.get("COMPANYID").toString().equals("yangguangchedao")) {
               dataList.add("'斑马快跑'");
           }else if (map.get("COMPANYID").toString().equals("1101FDZH891N")) {
               dataList.add("'飞嘀智慧'");
           }else if (map.get("COMPANYID").toString().equals("jbyy")) {
               dataList.add("'久柏'");
           }else if (map.get("COMPANYID").toString().equals("yuedao")) {
               dataList.add("'美跃'");
           }else if (map.get("COMPANYID").toString().equals("2201JQCXH93U")) {
               dataList.add("'吉汽'");
           }
       }
       cyxt.setDataItem1(dataList.toString().replace(" ",""));
       for (int i = 0; i < 10; i++) {
           Object data = distributionOrderList.get(i).get("C");
           list1.add(data.toString());
       }
       cyxt.setDataItem2(list1.toString().replace(" ",""));
       String data = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
       list.add(data);
   }









    /**
     * Methad info://通用参数
     * @author 流光亦断
     * @date 2022/1/6 18:28
     */

    public Cyxt buildgeneralpParame(String id, String mainType, String subType) {
        Cyxt cyxt = new Cyxt();
        cyxt.setId(id);
        cyxt.setMainType(mainType);
        cyxt.setSubType(subType);
        return cyxt;
    }


    //计算天数的方法
    public int getDays(int year, int month) {
        int days = 0;
        for (int i = 0; i < month; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) {
                return 31;
            } else if (i == 3 || i == 5 || i == 8 || i == 10) {
                return 30;
            } else {
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    return 29;
                } else {
                    return 28;
                }
            }
        }
        return days;
    }


}
